# coding:utf-8
import time
from fabric.api import *
from fabric.colors import *
import setting

config = None


def init_evn():
    global config
    config = setting.config
    # 本地开发服务器信息
    env.local_package_path = config.LOCAL_PACKAGE_PATH
    env.local_package_folder = config.LOCAL_PACKAGE_FOLDER
    env.time = int(time.strftime("%Y%m%d")) * 100

    env.roledefs = {
        'jumpserver': [config.JUMP_IP],
    }

    # 远程服务器信息
    env.hosts = config.SERVER_IPS.append(config.JUMP_IP)
    env.port = config.PORT
    env.user = 'root'
    env.jump_tmp_folder = config.JUMP_TMP_FOLDER
    if config.PASSWORD:
        env.password = config.PASSWORD
    else:
        env.use_ssh_config = True
        env.key_filename = config.LOCAL_KEY_FILE_PATH


# 本地开始打包
@roles('jumpserver')
def package(api_env='prod'):
    print yellow('<------------打包开始------------>')
    with lcd('%s' % env.local_package_path):
        local('mvn clean package -P %s' % api_env)
    print yellow('<------------完成完成------------>')


# 删除依赖jar
@roles('jumpserver')
def package_jar_not_depend():
    print yellow('<------------删除lib包开始------------>')
    for path in config.LOCAL_PACKAGE_EXCLUDER:
        local('rm -rf %s' % ('%s/%s' % (env.local_package_path, path)))
    print yellow('<------------删除lib包完成------------>')


# 项目打包成tar.gz包
@roles('jumpserver')
def project_to_targz():
    with lcd("%s/target" % env.local_package_path):
        local('tar -czvf %s %s' % (
            env.local_package_folder + config.LOCAL_PACKAGE_SUFFIX, env.local_package_folder))
        print yellow('<------------打tar.gz包完成------------>')


# 本地开始上传
@roles('jumpserver')
def up_package_to_jump():
    print blue('<------------上传jar包开始------------>')
    local_package_name = env.local_package_folder + config.LOCAL_PACKAGE_SUFFIX
    remote_file_path, remote_package_name = remote_file_not_exist_path(env.jump_tmp_folder, env.time,
                                                                       env.local_package_folder + config.LOCAL_PACKAGE_SUFFIX)
    put('%s/target/%s' % (env.local_package_path, local_package_name), remote_file_path)
    print blue('<------------上传jar包完成------------>')
    return remote_package_name


@roles('jumpserver')
def up_python_to_jump(path):
    print blue('<------------上传python开始------------>')
    if not remote_file_exist(config.JUMP_PYTHON_FILE_PATH):
        run('mkdir %s' % config.JUMP_PYTHON_FILE_PATH)

    put('%s/jump.py' % path, '%s/jump.py' % config.JUMP_PYTHON_FILE_PATH)
    put('%s/setting.py' % path, '%s/setting.py' % config.JUMP_PYTHON_FILE_PATH)
    print blue('<------------上传python完成------------>')
    return ''


# 判断远程war包是否存在
@roles('jumpserver')
def remote_file_not_exist_path(dir_path, flag, file_name):
    file_path = '%s/%s-%s' % (dir_path, flag, file_name)
    if int(run(" [ -e '%s' ] && echo 11 || echo 10" % file_path)) == 11:
        flag += 1
        return remote_file_not_exist_path(dir_path, flag, file_name)
    else:
        return file_path, '%s-%s' % (flag, file_name)


def remote_file_exist(path):
    if int(run(" [ -e '%s' ] && echo 11 || echo 10" % path)) == 11:
        return True
    else:
        return False


# 执行跳板机脚本,上传跳板机文件到真是服务器
@roles('jumpserver')
def jump_file_to_server(remote_package_name):
    logoff = "fab -f %s/jump.py up_file_to_server:%s" % (
        config.JUMP_PYTHON_FILE_PATH, remote_package_name)
    print yellow(logoff)
    run(logoff)


# 执行远程备份ROOT文件夹脚本
@roles('jumpserver')
def jump_back_file():
    logoff = 'fab -f %s/jump.py server_back_file' % config.JUMP_PYTHON_FILE_PATH
    print yellow('开始执行<---------%s----------->命令' % logoff)
    run(logoff)


# 执行远程命令
@roles('jumpserver')
def remote_run_command(path, logoff):
    command = "fab -f %s/jump.py server_run_command:path='%s',logoff='%s'" % (
        config.JUMP_PYTHON_FILE_PATH, path, logoff)
    run(command)


@roles('jumpserver')
def server_back_file():
    remote_file_path, remote_package_name = remote_file_not_exist_path(config.SERVER_BAK_PATH, env.time,
                                                                       config.SERVER_BAK_FOLDER + config.LOCAL_PACKAGE_SUFFIX)
    with cd(config.SERVER_BAK_PATH):
        command = 'tar -czvf %s %s' % (remote_package_name, config.SERVER_BAK_FOLDER)
        print red('开始执行<---------%s----------->命令' % command)
        run(command)


@roles('jumpserver')
def server_deploy_file(package):
    print red('<------------开始部署%s------------>' % package)
    if config.LOCAL_PACKAGE_FOLDER == 'classes':
        run('rm -rf %s/ROOT/WEB-INF/classes' % config.SERVER_BAK_PATH)
        with cd(config.SERVER_TMP_FOLDER):
            run('tar -xzvf %s -C %s/ROOT/WEB-INF' % (package, config.SERVER_BAK_PATH))
    elif config.LOCAL_PACKAGE_FOLDER == 'ROOT':
        for path in config.SERVER_TOMCAT_DELETE_PATHS:
            run('rm -rf %s/path' % (config.SERVER_TOMCAT_PATH,path))
        pass
    else:
        return ''
    print red('<------------部署%s文件完成------------>' % package)


@roles('jumpserver')
def server_restart():
    print red('<------------开始重启服务------------>')
    with cd(config.SERVER_TOMCAT_PATH):
        run('./bin/shutdown.sh')
        run('set -m;./bin/startup.sh')
    print red('<------------重启服务完成------------>')


@roles('jumpserver')
def server_run_command(path, logoff):
    print red('开始执行<---------%s----------->命令' % path)
    with cd(path):
        print red(logoff)
        run(logoff)